function generate_model_structures(PV, dynareFileName,num)

% Christoph Gortz
% 18.04.2012
%
% This function is called by initialise_spectral_decomposition.m. It uses
% the parameter values and calls dynare to generate associated model 
% structures.



%% UNPACK AND SAVE PARAMETERS
% Unpacks and saves parameter names and values so that they can be loaded
% by the dynare file

% Get the field names of the structure.
eval([ 'fieldsAll = fieldnames(PV.parameterValues' num ', ''-full'');']);
% Find out how many there are - for our loop.
numberOfFields = length(fieldsAll);

for f = 1 : numberOfFields
    thisField = fieldsAll{f};
    eval([ 'fieldValue = PV.parameterValues' num '.' thisField ';']);
    eval([ fieldsAll{f} '= fieldValue;']);
end
% Release temporary variables.
clear('f', 'thisField', 'numberOfFields');
clear('fieldsAll', 'commandLine'); 

save ParameterValuesForDynare


%% SIMULATE TO GENERATE TIME SERIES
% Run the dynare file that simulates the series and save the desired series

eval([ 'dynare ' dynareFileName]);

delete ParameterValuesForDynare.mat
end